/*#################################################

Tabs | v1.4.2
Max Felker | max@bigroomstudios.com
Brian Leighton | brian@bigroomstudios.com

Creates tabbed content

#################################################*/
 
// START Class
var Tabs = Class.create({

	initialize: function(config) {
	
		// containers 
		this.container = $(config.container);
		
		// tabs
		this.tab_elements = $$('#'+config.container+' .tab');
		this.tabs_count = this.tab_elements.length;
		this.tabs_current_index = 0;
		this.tabs = $A();
		
		this.counter = 0;
		
		this.tab_elements.each(function(tab) {

			var tab_id = tab.id.gsub('-tab','');
			tab.panel = $(tab_id+'-panel');
			tab.panel.hide();
			tab.index = this.counter;
			this.tabs[tab.index] = tab;
			this.counter++;
		
		}.bind(this));
		
		this.counter = 0;
		
		this.tabs_current_index = 0;
		
		this.current_tab = this.tabs[this.tabs_current_index];
		
		this.show_tab();
		
		this.tabs.each(function(tab) { 
		
			tab.on('click', function() {
			
				this.tabs_current_index = tab.index;
				this.show_tab();
			
			}.bind(this));
		
		}.bind(this));

	
	}, // END init
	
	show_tab: function() {

		this.current_tab.panel.hide();
		this.current_tab.removeClassName('current');
		
		this.current_tab = this.tabs[this.tabs_current_index];
		
		this.current_tab.panel.show();
		this.current_tab.addClassName('current');
	
	},
	
	next_tab: function() {
	
		this.tabs_current_index = (this.tabs_current_index+1 + this.tabs_count) % this.tabs_count;
		
		this.show_tab();
	
	},
	
	previous_tab: function() {
		
		this.tabs_current_index = (this.tabs_current_index-1 + this.tabs_count) % this.tabs_count;
		
		this.show_tab();
	
	}
});
// END CLass
